package com.edu.realtime.util;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created on 2022/10/20.
 *
 * @author Topus
 */
public class ThreadPoolUtil {
    private static volatile ThreadPoolExecutor poolExecutor;

    public static ThreadPoolExecutor getInstance() {
        if (poolExecutor == null) {
            synchronized (ThreadPoolUtil.class) {
                if (poolExecutor == null) {
                    // 创建线程池
                    poolExecutor = new ThreadPoolExecutor(
                        // 初始化pool数量
                        4,
                        // pool中最大的连接数
                        20,
                        // 保持连接时长
                        60 * 5,
                        // 连接时长单位
                        TimeUnit.SECONDS,
                        // 连接池对象存储的队列
                        new LinkedBlockingDeque<>(Integer.MAX_VALUE));

                }
            }
        }
        return poolExecutor;
    }
}
